/**
 * 
 */

package com.shy.portal.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.shy.portal.entity.Menu;
import com.shy.portal.vo.RoleMenuVo;

/**
 * <p>
 * Title: Data2TreeVoUtils
 * </p>
 * <p>
 * Description:数据轉換為Easyui樹形結構工具类
 * </p>
 * <p>
 * Company:ipebg
 * </p>
 * 
 * @author H2013788
 */
public class Data2TreeVoUtils {

    /**
     * 通过角色权限關係列表和全部权限列表進行組裝，標識出选中状态
     * 
     * @param maxLevel
     *            当前系统中最大菜单级別
     * @param roleMenuList
     *            当前角色具有的权限列表
     * @param menuList
     *            全部权限列表
     * @return 角色菜单列表
     * @author H2013788
     * @date 2017/10/11下午1:54:15
     */
    public static List<RoleMenuVo> buildMenuTreeVo(int maxLevel, List<Menu> roleMenuList,
            List<Menu> menuList) {
        if (menuList == null || menuList.isEmpty()) {
            return null;
        }
        List<RoleMenuVo> list = new ArrayList<RoleMenuVo>();
        for (Menu menu : menuList) {
            // 樹形菜单默認处理一级权限（菜单），子菜单通过遞歸查询
            if (null != menu.getLevel() && menu.getLevel() == 1) {
                Map<String, Object> map = new HashMap<>();
                map.put("level", menu.getLevel());
                map.put("sort", menu.getSort());
                map.put("code", menu.getMenuCode());
                map.put("url", menu.getMenuUrl());
                RoleMenuVo roleMenuVo = new RoleMenuVo();
                roleMenuVo.setId(menu.getId());
                roleMenuVo.setText(menu.getMenuName());
                roleMenuVo.setChildren(getMenuChildren(maxLevel, menu, roleMenuList,
                        menuList));
                roleMenuVo.setChecked(ischeckedMenu(menu, roleMenuList));
                roleMenuVo.setAttributes(map);
                list.add(roleMenuVo);
            }
        }
        return list;
    }

    /**
     * 判斷当前权限是否在选中列表中
     * 
     * @param menu
     *            菜单
     * @param checkedMenuList
     *            已选择菜单列表
     * @return 菜单
     * @author H2013788
     * @date 2017/10/11上午10:34:27
     */
    private static boolean ischeckedMenu(Menu menu, List<Menu> checkedMenuList) {
        if (checkedMenuList == null) {
            return false;
        } else {
            boolean checked = false;
            for (Menu menuTmp : checkedMenuList) {
                if (menuTmp.getId().equals(menu.getId())) {
                    checked = true;
                }
            }
            return checked;
        }
    }


    /**
     * 遞歸組裝权限列表，设置子权限列表
     * 
     * @param maxLevel
     *            当前系统中最大菜单级別
     * @param menu
     *            当前权限
     * @param roleCheckedMenuList
     *            当前角色具有的权限列表
     * @param menuList
     *            全部权限列表
     * @return 角色菜单
     * @author H2013788
     * @date 2017/10/11下午1:59:28
     */
    private static List<RoleMenuVo> getMenuChildren(int maxLevel, Menu menu,
            List<Menu> roleCheckedMenuList, List<Menu> menuList) {
        // 如果当前权限级別已經是最高级別，則不存在子節點
        if (menu.getLevel() >= maxLevel) {
            return null;
        } else {
            List<RoleMenuVo> list = new ArrayList<RoleMenuVo>();
            for (Menu menuChild : menuList) {
                if (null != menuChild.getParent()
                        && menuChild.getParent().getId().equals(menu.getId())) {
                    Map<String, Object> map = new HashMap<>();
                    map.put("level", menuChild.getLevel());
                    map.put("sort", menuChild.getSort());
                    map.put("code", menuChild.getMenuCode());
                    map.put("url", menuChild.getMenuUrl());
                    RoleMenuVo roleMenuVo = new RoleMenuVo();
                    roleMenuVo.setId(menuChild.getId());
                    roleMenuVo.setText(menuChild.getMenuName());
                    roleMenuVo.setChildren(getMenuChildren(maxLevel, menuChild,
                            roleCheckedMenuList, menuList));
                    roleMenuVo.setChecked(ischeckedMenu(menuChild, roleCheckedMenuList));
                    roleMenuVo.setAttributes(map);
                    list.add(roleMenuVo);
                }
            }
            return list;
        }
    }

    /**
     * 構建页面左側樹形菜单
     * 
     * @param list
     *            角色菜单关系列表
     * @return 菜单
     * @author H2013788
     * @date 2017/11/16上午10:30:00
     */
    public static String buildTreeMenu(List<RoleMenuVo> list) {
        String treeHtml = "";
        if (null != list && !list.isEmpty()) {
            for (RoleMenuVo roleMenuVo : list) {
                String currentNodeHtml = "<div title=\"" + roleMenuVo.getText()
                        + "\" iconCls=\"icon-blank\" class=\"cs-navi-tab\" "
                        + "style=\"padding:1px;overflow:auto\">";
                if (null != roleMenuVo.getChildren() && !roleMenuVo.getChildren().isEmpty()) {
                    currentNodeHtml += buildMenuNode(roleMenuVo);
                }
                currentNodeHtml += "</div>";
                treeHtml += currentNodeHtml;
            }
        }
        return treeHtml;
    }

    /**
     * 遞歸組裝子節點
     * 
     * @param roleMenuVo
     *            角色菜单关系对象
     * @return 菜单
     * @author H2013788
     * @date 2017/11/16上午10:34:39
     */
    private static String buildMenuNode(RoleMenuVo roleMenuVo) {
        String nodeHtml = "<ul class=\"easyui-tree base-side-tree\">";
        for (RoleMenuVo nodeVo : roleMenuVo.getChildren()) {
            nodeHtml += "<li iconCls=\"icon-book\">";
            if (null != nodeVo.getChildren() && !nodeVo.getChildren().isEmpty()) {
                nodeHtml += "<span>" + nodeVo.getText() + "</span>";
                nodeHtml += buildMenuNode(nodeVo);
            } else {
                nodeHtml += "<a href=\"javascript:void(0)\" onclick=\"addTab('" + nodeVo.getText()
                        + "','" + nodeVo.getAttributes().get("url") + "')\" iframe=\"1\">"
                        + nodeVo.getText() + "</a>";
            }
            nodeHtml += "</li>";
        }
        nodeHtml += "</ul>";
        return nodeHtml;
    }
    
}
